package cn.lanca.esaypoi.c202204.week_4.sun;

import cn.lanca.esaypoi.c202204.week_3.v4.CommonA;
import cn.lanca.esaypoi.util.tool.CommonAll;
import cn.lanca.esaypoi.util.tool.ExcelUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * project name:<font size = "1"><b>小芄健康数据运营平台</b></font><br>
 * file name:<font size = "1"><b> </b></font><br>
 * description:<font size = "1"><b> </b></font><br>
 * company name:<font size = "1"><b>小芄医药科技（成都）有限公司-研发中心</b></font><br>
 *
 * @author <a href="mail to: meihongliang@xiaowanjiankang.com" rel="nofollow">meihongliang<br>
 * @version 1.0.0<br>
 * @since [1] [2022/4/24 11:07] [meihongliang] [新建] <br>
 */
public class Test {
    public static void main(String[] args) throws IOException {
//        downloadWithPic();

        try {
            System.out.println("before");
            long adb = Long.parseLong("adb");
            System.out.println("after");
        } catch (NumberFormatException e) {
//            throw new RuntimeException(e);
        }
    }

    /**
     * 下载图片，返写图片名称字段
     *
     * @throws IOException
     */
    private static void downloadWithPic() throws IOException {
        List<CommonA> commonBS = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/2022-04-14/week_4/sun/B_女性常用OTC71_匹配上数据_12.xlsx", CommonA.class);
        List<CommonA> commonCS = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/2022-04-14/week_4/sun/C_肛肠常用OTC64_匹配上数据_13.xlsx", CommonA.class);
        List<CommonA> commonDS = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/2022-04-14/week_4/sun/D_儿童常用OTC77_匹配上数据_28.xlsx", CommonA.class);
        System.out.println(commonBS.size());
        System.out.println(commonCS.size());
        System.out.println(commonDS.size());
        Boolean flag1 = ExcelUtil.downLoadPicWithList(commonBS, "/Users/meihongliang/Downloads/A_xiaowan/2022-04-14/week_4/sun/B_女性常用/", "/Users/meihongliang/Downloads/A_xiaowan/2022-04-14/week_4/sun/带图片名称_B_女性常用OTC71_匹配上数据_12.xlsx");
        Boolean flag2 = ExcelUtil.downLoadPicWithList(commonBS, "/Users/meihongliang/Downloads/A_xiaowan/2022-04-14/week_4/sun/C_肛肠常用/", "/Users/meihongliang/Downloads/A_xiaowan/2022-04-14/week_4/sun/带图片名称_C_肛肠常用OTC64_匹配上数据_13.xlsx");
        Boolean flag3 = ExcelUtil.downLoadPicWithList(commonBS, "/Users/meihongliang/Downloads/A_xiaowan/2022-04-14/week_4/sun/D_儿童常用/", "/Users/meihongliang/Downloads/A_xiaowan/2022-04-14/week_4/sun/带图片名称_D_儿童常用OTC77_匹配上数据_28.xlsx");
        System.out.println(flag1);
        System.out.println(flag2);
        System.out.println(flag3);
    }

    /**
     * 一个文件,多个sheet
     *
     * @throws IOException
     */
    private static void writeOneFileMaySheets() throws IOException {
        // #1.全部数据准备
        List<CommonAll> allDBData = ExcelUtil.getAllDBData();
        System.out.println(allDBData.size());
        allDBData.forEach(e -> {
            e.setKey2(e.getA4() + "_" + e.getA13());
            e.setKey3(e.getA4() + "_" + e.getA5() + "_" + e.getA13());
        });

        // /Users/meihongliang/Downloads/A_xiaowan/download/建设路院内商城选品415筛选.xlsx
        List<CommonABCDEFG> commonAS = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/download/建设路院内商城选品415筛选.xlsx", CommonABCDEFG.class, 0);
        List<CommonABCDEFG> commonBS = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/download/建设路院内商城选品415筛选.xlsx", CommonABCDEFG.class, 1);
        List<CommonABCDEFG> commonCS = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/download/建设路院内商城选品415筛选.xlsx", CommonABCDEFG.class, 2);
        List<CommonABCDEFG> commonDS = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/download/建设路院内商城选品415筛选.xlsx", CommonABCDEFG.class, 3);
        List<CommonABCDEFG> commonES = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/download/建设路院内商城选品415筛选.xlsx", CommonABCDEFG.class, 4);
        List<CommonABCDEFG> commonFS = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/download/建设路院内商城选品415筛选.xlsx", CommonABCDEFG.class, 5);
        List<CommonABCDEFG> commonGS = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/download/建设路院内商城选品415筛选.xlsx", CommonABCDEFG.class, 6);
        HashMap<String, List<CommonABCDEFG>> listHashMap = new HashMap<>();
        // 所有sheet存入
        listHashMap.put("A_大健康97", commonAS);
        listHashMap.put("B_女性常用OTC71", commonBS);
        listHashMap.put("C_肛肠常用OTC64", commonCS);
        listHashMap.put("D_儿童常用OTC77", commonDS);
        listHashMap.put("E_女性常用其他54", commonES);
        listHashMap.put("F_肛肠常用其他8", commonFS);
        listHashMap.put("G_儿童常用其他44", commonGS);


        HashMap<String, List<CommonABCDEFG>> result = new HashMap<>();
        LinkedHashMap<String, List<CommonABCDEFG>> sheetsMap = new LinkedHashMap<>();
        listHashMap.forEach((k, v) -> {
            System.out.println(k + "前:" + v.size());
            v.forEach(e -> {
                e.setKey2(e.getA1() + "_" + e.getA3());
                e.setKey3(e.getA1() + "_" + e.getA2() + "_" + e.getA3());
            });
            // 准备完毕
            AtomicInteger count2 = new AtomicInteger(0);
            AtomicInteger count3 = new AtomicInteger(0);
            List<CommonAll> resultCommonA = new LinkedList<>();
            List<CommonABCDEFG> result3 = new LinkedList<>();

            allDBData.forEach(e -> {
                Iterator<CommonABCDEFG> iterator = v.iterator();
                while (iterator.hasNext()) {
                    CommonABCDEFG m = iterator.next();
                    if (e.getKey3().equals(m.getKey3())) {
                        count3.incrementAndGet();
                        result3.add(m);
                        resultCommonA.add(e);
                        iterator.remove();
                    }
                }
                //
                // System.out.println(k + "后:" + v.size());
//                ExcelUtil.writeExcel();
            });

            sheetsMap.put(k, v);
            System.out.println(k + "后hou:" + v.size());
            System.out.println();
            if (result3.size() > 0) {
                // 循环处理各个sheet
                System.out.println(k);
                System.out.println("3:" + count3);
                System.out.println("result3:" + result3.size());
                System.out.println("resultCommonA:" + resultCommonA.size());
                // ExcelUtil.writeExcel(resultCommonA, CommonAll.class, "/Users/meihongliang/Downloads/A_xiaowan/download/" + k + "_匹配上数据_" + resultCommonA.size() + ".xlsx");
                result.put(k, result3);
            }
        });

        ExcelUtil.writeSheetsToOneFIle("/Users/meihongliang/Downloads/A_xiaowan/download/未匹配上数据库数据的415表剩余数据.xlsx", sheetsMap, CommonABCDEFG.class);
        System.out.println("end");
    }
}
